<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class GoodsComment extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'goods_comment';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getCommentImagesAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['commentimages']) ? $data['commentimages'] : '');
        if(empty($value)){ return  [];}
        $value=explode(',',$value);
        foreach ($value as $k=>&$v){
            $v=get_app_image_url($v);
        }
        return $value;
    }
    /**
     * 评价
     * $order_goods 订单商品 $note 评论内容 $commentimages 图片 $user_id 评论用户
     * */
    public static function evaluate($order_goods,$note,$commentimages,$star,$user_id){
        $data['order_id']=$order_goods['order_id'];
        $data['order_goods_id']=$order_goods['goods_id'];
        $data['type']=$order_goods['type'];
        $data['type']=$order_goods['type'];
        $data['gid']=$order_goods['goods_id'];
        $data['sku_id']=$order_goods['sku_id'];
        $data['goods_name']=$order_goods['goods_name'];
        $data['goods_sku_name']=$order_goods['sku_name'];
        $data['note']=$note;
//        $data['note']=$order_goods['sku_name'];
        $data['status']=1;
        $data['commentimages']=$commentimages;
        $data['user_id']=$user_id;
        $data['star']=$star;
        return self::create($data);
    }

    /**
     * 评价详情
     * */
    public static function getInfo($id){
        $where['goods_comment.id']=$id;
        $info =  self::where($where)
            ->with('user')
            ->find();
        if($info){
            $info->getRelation('user')->visible(['nickname','avatar']);
            $info['createtime'] = date('Y-d-d H:i:s',$info['createtime']);
        }

        return $info;
    }

    /**
     * 评价删除
     * */
    public static function getDel($ids,$user_id){
//        $where['id']=$ids;
        $where['user_id']=$user_id;
        $data['deletetime'] = time();
        $data['status'] = '0';
        $info =  self::where($where)
            ->where('id','in',$ids)
            ->update($data);
//        $info->getRelation('user')->visible(['nickname','avatar']);
//        $info['createtime'] = date('Y-d-d H:i:s',$info['createtime']);
        return $info;
    }
    /**
     * 商品评价列表
     * */
    public static function getList($gid,$type){
        $where['gid'] = $gid;
        $where['goods_comment.type'] = $type;
        $list = self::where($where)
            ->with(['user'])
            ->order('goods_comment.id desc')
            ->paginate();
        foreach ($list as $k=>$v){
            $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
            $v->getRelation('user')->visible(['nickname','avatar']);
        }
        return $list;
    }


    /**
     * 我的商品评价列表
     * */
    public static function getMyList($user_id){
//        $where['gid'] = $gid;
//        $where['goods_comment.type'] = $type;
        $where['goods_comment.user_id'] = $user_id;
        $list = self::where($where)
            ->with(['user'])
            ->order('goods_comment.id desc')
            ->paginate();
        foreach ($list as $k=>$v){
            $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
            $v->getRelation('user')->visible(['nickname','avatar']);
        }
        return $list;
    }

}
